export function buildFullUrl(url, auth) {
    const arr = [];
    for (const key in auth) {
      arr.push(`${key}=${encodeURIComponent(auth[key])}`);
    }
    return `${url}?${arr.join('&')}`;
}

export function isJsonString(str) {
  try {
    const obj = JSON.parse(str);
    return typeof obj === 'object' && obj !== null;
  } catch (e) {
    return false;
  }
}


export async function getToken(appid, accessKey) {
  // https://openspeech.bytedance.com/api/v1/sts/token
  const result = await fetch('/api/v1/sts/token', {
    method: 'POST',
    headers: {
      Authorization: `Bearer; ${accessKey}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      appid,
      duration: 3600,  // 单位秒，默认1小时
    }),
  })
    .then(res => res.json())
    .then(res => res.jwt_token);
  return result;
}


// const appid = '5075494287'
// const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiNTA3NTQ5NDI4NyJdLCJleHAiOjE3NDc5MzQyMDN9.-4DRE1ViEaJw_6ud9gyJIMUB5-CSHDkuHLmiLeoSaJ0';